package com.chatdating.controller;


import com.chatdating.entity.Member;
import com.chatdating.entity.User;
import com.chatdating.entity.dto.RegisterDto;
import com.chatdating.entity.pojo.Result;
import com.chatdating.entity.vo.LoginInVo;
import com.chatdating.entity.vo.UserVo;
import com.chatdating.mapper.UserMapper;
import com.chatdating.redis.RedisComponet;
import com.chatdating.service.MemberService;
import com.chatdating.service.UserService;
import com.chatdating.utils.EmptyUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private MemberService memberService;
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisComponet redisComponet;


    /**
     * 获取用户信息  (包括是不是会员信息)
     * @param username
     * @return
     */
   @PostMapping("/getinfo")
    public Result getUser(String username){
       UserVo userVo = new UserVo();
       User byNmme = userService.findByNmme(username);
       BeanUtils.copyProperties(byNmme,userVo);
       Member byUid = memberService.getByUid(byNmme.getId());
       if(EmptyUtil.isNotEmpty(byUid)){
           //如果查到用户有会员信息则代表是会员
           userVo.setMemberStatus(1);
       }
       userVo.setMemberStatus(0);
       return Result.success(userVo);
   }

    @GetMapping("/vip-expire-time")
    public Result getVip(
            HttpServletRequest request ){
        String token = tockens(request);
        LoginInVo tokenUserInfoDto = redisComponet.getTokenUserInfoDto(token);
        String username = tokenUserInfoDto.getUsername();
        User byName = userMapper.findByName(username);
        int id = byName.getId();

        Member byUid = memberService.getByUid(id);
        LocalDateTime expireTime = byUid.getEndTime();

        //查询 vip用户的到期时间

        return Result.success(expireTime);
    }

    /**
     * 用户修改基本信息
     * @param user
     * @return
     */
   @PostMapping("/updateUserInfo")
   public Result updataUsetrInfor(@RequestPart(value = "user") User user,
                                  @RequestPart( required = false) MultipartFile head){    // 允许修改头像
        if(EmptyUtil.isEmpty(user)){
            return Result.error(400,"用户为空");
        }
        if(EmptyUtil.isEmpty(user.getUsername())){
            return Result.error(400,"用户名为必传参数");
        }

       User byNmme = userService.findByNmme(user.getUsername());
       if(EmptyUtil.isEmpty(byNmme)){
           return Result.error(400,"用户不存在");
       }
       userService.updateUserInfo(user,head);
       return null;
   }
   @GetMapping("/checkUsername")
   public Result checkUsername(@RequestParam String username){
       User byNmme = userService.findByNmme(username);
       if(EmptyUtil.isEmpty(byNmme)){
           return Result.success();
       }
       return Result.error(400,"用户名已存在");
   }

    public String tockens(HttpServletRequest request){
        String authHeader = request.getHeader("Authorization");
        String token = authHeader.substring(7);
        return token;
    }

}
